package managers
{
	import Utils.CommonInfoManager;
	
	import com.adobe.air.notification.AbstractNotification;
	import com.adobe.air.notification.Notification;
	import com.adobe.air.notification.NotificationClickedEvent;
	import com.adobe.air.notification.Purr;
	import com.asfusion.mate.events.Dispatcher;
	
	import events.Me2DayAPIEvent;
	
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import me2day.api.data.Me2dayCommentBy;
	import me2day.api.data.Me2dayCommentLogs;
	
	import mx.formatters.DateFormatter;
	
	public class AutoUpdateManager
	{
		public function AutoUpdateManager()
		{
			if ( instance )
			{
				throw new Error("use GetInstance function to get singleton instance of AutoUpdateManager class.");
			}
			
			timer.addEventListener(TimerEvent.TIMER, OnTimer);
		}
		
		private static var instance:AutoUpdateManager;
		
		public static function GetInstance():AutoUpdateManager
		{
			if ( !instance )
			{
				instance = new AutoUpdateManager;
			}
			return instance;
		}
		
		private var timer:Timer = new Timer(0, 0);
		
		public function StartAutoUpdate():void
		{
			return;
			//////////
			
			if ( timer.running )
				timer.stop();
				
			//timer.delay = CommonInfoManager.autoUpdateDuration * 1000 * 60;
			//timer.repeatCount = 1;
			timer.delay = 1000 * 15;
			timer.start();
		}
		
		private var dispatcher:Dispatcher = new Dispatcher;
		private var isFirst:Boolean = true;
		private function OnTimer(event:TimerEvent):void
		{
			dispatcher.dispatchEvent(new Me2DayAPIEvent(Me2DayAPIEvent.TRACK_COMMENTS));
		}
		
		private var lastCommentedTime:Date = new Date;
		public function UpdateCommentLogs(commentLogs:Me2dayCommentLogs):void
		{
			var newCommentLogs:Array = [];
			var item:Me2dayCommentBy;
			for each ( item in commentLogs.commentToMeArr )
			{
				var pubDate:Date = getDateFromString(item.comment.pubDate);
				//if ( pubDate.time > lastCommentedTime.time )
					newCommentLogs.push(item);
				//else
				//	break;
			}
			
			item = commentLogs.commentToMeArr[0];
			lastCommentedTime = getDateFromString(item.comment.pubDate);
			
			var comments:String = "";
			for each ( item in newCommentLogs )
			{
				comments += item.post.body + "\n\t" + item.comment.body+"by "+item.comment.author.nickname+"\n";
				
			}
			
			 var n:Notification = new Notification("댓글 알림", "", CommonInfoManager.notifyWindowPos, 30);
			n.htmlText = comments;
			//n.id = String(count);
			n.addEventListener(NotificationClickedEvent.NOTIFICATION_CLICKED_EVENT,
				function(e:NotificationClickedEvent):void
				{
					trace("You clicked on notification: " + AbstractNotification(e.target).id);
				});
			purr.addNotification(n); 
		}
		private var purr:Purr = new Purr(1);
		
		
		private var reg:RegExp = new RegExp("-", "g");
		private function getDateFromString(value:String):Date
		{
			var datestr:String = value.substr(0, 10).replace(reg, '/') + " " + value.substr(11, 8) + " GMT+0000";
			
			/* var f:DateFormatter = new DateFormatter;
			f.formatString = "YYYY/MM/DD A L:NN";
			trace("formatted date:" + f.format(new Date(Date.parse(datestr)))); */
			
			return new Date(Date.parse(datestr));		
		}
	}
}